package edu.bit.water.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.bit.water.entity.MetaEvaWsrcInfo;
import edu.bit.water.service.MetaEvaWsrcInfoAssociationService;
import edu.bit.water.service.MetaEvaWsrcInfoService;

@Controller
@RequestMapping("/meta_eva_wsrc_info")
public class MetaEvaWsrcInfoController {

	//记得加上这个哇，这是告诉全世界，你要开始在这类里面使用日志
	 protected static final Logger logger = LoggerFactory.getLogger(MetaEvaWsrcInfoController.class);
	 
	@Autowired
	private MetaEvaWsrcInfoService metaEvaWsrcInfoService;
	@Autowired
	private MetaEvaWsrcInfoAssociationService metaEvaWsrcInfoAssociationService;
	/**
	 ** 查询元数据表
	 **
	 * @return Map<String, Object> 如果查询成功则success为true,并返回metaSensorList
	 *         如果失败则返回false,errMsg中为结果
	 */
	@ResponseBody
	@RequestMapping(value = "/select", method = RequestMethod.GET)
	public Map<String, Object> queryMetaEvaWsrcInfo() {
		Map<String, Object> modelMap = new HashMap<>();
		List<MetaEvaWsrcInfo> metaEvaWsrcInfoList = new ArrayList<MetaEvaWsrcInfo>();
		try {
			metaEvaWsrcInfoList = metaEvaWsrcInfoService.queryMetaEvaWsrcInfo();
//			ObjectMapper mapper = new ObjectMapper();
//			String jsonlist = mapper.writeValueAsString(metaEvaWsrcInfoList); 
//			logger.info("");
//			logger.info("controll结果json:"+jsonlist);
			modelMap.put("status", "success");
			modelMap.put("rows", metaEvaWsrcInfoList);
		} catch (Exception e) {
			modelMap.put("status", "failed");
			modelMap.put("msg", e.getMessage());
		}
		return modelMap;
	}
	/**
	 * 查詢組號里列數
	 * @param faLevel 組號
	 * @return 查詢操作後信息
	 */
	@ResponseBody
	@RequestMapping(value = "/selectColumnNumByFaLevel", method = RequestMethod.GET)
	public Map<String, Object> queryColumnNumByFaLevel(Integer faLevel) {
		Map<String, Object> modelMap = new HashMap<>();
		try {
			int num = metaEvaWsrcInfoService.queryColumnNumByFaLevel((int)faLevel);
			modelMap.put("status", "success");
			modelMap.put("data", num);
		} catch (Exception e) {
			modelMap.put("status", "failed");
			modelMap.put("msg", e.getMessage());
		}
		return modelMap;
	}
	
	/**
	 * 
	 * @param metaMetaEvaWsrcInfo 元数据水平衡评估表实体类
	 * @return 成功返回1，失败返回0
	 */
	@ResponseBody
	@RequestMapping("/insert")
	public Map<String, Object> insertMetaEvaWsrcInfo(MetaEvaWsrcInfo metaEvaWsrcInfo) {
		MetaEvaWsrcInfo temp = metaEvaWsrcInfo;
		return metaEvaWsrcInfoService.insertMetaEvaWsrcInfo(temp);
	}

	/**
	 *
	 * @param metaMetaEvaWsrcInfo 元数据水平衡评估表实体类
	 * @return 成功返回1，失败返回0
	 */
	@ResponseBody
	@RequestMapping("/update")
	public Map<String, Object> updateMetaEvaWsrcInfo(MetaEvaWsrcInfo metaEvaWsrcInfo) {
		MetaEvaWsrcInfo temp = metaEvaWsrcInfo;
		return metaEvaWsrcInfoService.updateMetaEvaWsrcInfo(temp);
	}

	/**
	 * 
	 * 
	 * @param metaSensor 元数据水平衡评估表实体类
	 * @return 成功返回1，失败返回0
	 * 改进：可以将关联表的数据删除放入service层
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public Map<String, Object> deleteMetaEvaWsrcInfo(@RequestBody List<String> enNameList) {
		int flag = metaEvaWsrcInfoService.deleteMetaEvaWsrcInfo(enNameList);
		Map<String, Object> modelMap = new HashMap<>();
		if (flag == 1) {
			MetaEvaWsrcInfo metaEvaWsrcInfo = new MetaEvaWsrcInfo();
			for (String enName : enNameList) {
				metaEvaWsrcInfo.setEnName(enName);
				metaEvaWsrcInfoAssociationService.deleteMetaEvaWsrcInfoAssociation(metaEvaWsrcInfo);
			}
			modelMap.put("status", "success");
		}else {
			modelMap.put("status", "failed");
		}
		return modelMap;
	}
}